package org.jdcenter.server;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdcneter.system.Server;
import org.jdcneter.system.ServerConfig;
/**
 * @author miao
 * @since 2008-11-08
 *
 */
public abstract class AbstractServer implements Server{
	
	private static Log log = LogFactory.getLog(AbstractServer.class);
	
	private Properties props;

	@Override
	public void init(Properties props) {
		log.info("init ServerImpl");
		this.props = props;
		for(Object key:props.keySet()){
			System.setProperty(key.toString(), props.get(key).toString());
		}
	}

	@Override
	public void start() throws Exception{
		log.info("start ServerImpl");
		long startTime = System.currentTimeMillis();
		doStart();
		
	}
	
	protected abstract void doStart()throws Exception;
	
	protected URL getConfUrl() throws MalformedURLException{
		URL url = (URL)props.get(ServerConfig.CONF_URL);
		if(url == null){
			url = new URL(getHomeUrl(),ServerConfig.CONF_URL_SUFFIX);
		}
		return url;
	}
	
	protected URL getPluginsUrl(String url) throws MalformedURLException{
		return new URL(getConfUrl(),url);
	}
	
	protected URL getHomeUrl() throws MalformedURLException{
		String value = props.getProperty(ServerConfig.HOME_DIR);
		if(value != null){
			if(!value.endsWith("/")){
				value += "/";
			}
			return new URL(value);
		}
		return null;
	}

}
